
var curNode = null, treeNodes = null, callbackFunction = null, callbackFunctionSelect = null, treeNode0 = null, orgSelectNode=null;

//左侧树初始化
var orgTreeSetting = {
	view: {  
		showLine: true, 
		selectedMulti: false,  
		dblClickExpand: false  
	},  
	async: {
		enable: true,
		type: "get",
		dataType:"json",
		url:"/server/department/info/list.json",
		autoParam:["id=parentId"],
		dataFilter: filter
	},

	data: {  
		simpleData: {  
			enable: true,  
			idKey:"id",  
			pIdKey:"parentId",
			rootPId:0
		}
	},

	callback: {
		beforeClick:getCurrentNode
	}
};

//下拉树的初始化
var orgSelectTree = {
		view : {
			showIcon : true,
			showLine : true,
			dblClickExpand : false  
		},
		async : {
			enable : true,
			type : "get",
			dataType : "json",
			url : "/server/department/info/list.json",
			autoParam : [ "id=parentId" ],
		},
		data : {
			simpleData : {
				enable : true,
				idKey : "id",
				pIdKey : "parentId"
			}
		},
		callback : {
			onClick : selectTreeOnClick
		}
	};

	//选着下拉树操作
	function selectTreeOnClick(event, treeId, treeNode) {
		orgSelectNode = treeNode;
		zTreeOnClick(treeNode);
	}

	function zTreeOnClick(treeNode){}

function filter(treeId, parentNode, childNodes) {
	if (!childNodes) return null;
	for (var i=0, l=childNodes.length; i<l; i++) {
		//childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
		if (childNodes[i].parentId!='0') childNodes[i].open = false;
	}

	curChildNodes = childNodes;
	return childNodes;
}


function getCurrentNode(treeId, treeNode) {
	curNode = treeNode;
	loadTreeNode(treeNode.id,callbackFunction);
}

//初始化右侧咧条
function loadTreeNode(parentId,callback) {
	if(parentId==null) parentId=root;		
	$.getJSON("/server/department/info/list.json?parentId="+parentId,function(json){
		callback(json);
	});
}

function removeNode(id) {
	var node = treeNodes.getNodeByParam("id", id, null);
	treeNodes.removeNode(node);
}

//初始化树，包括回调函数
function initOrgTree(parentId,callback,anchor){

	//初始化0节点
	$.getJSON("/server/department/info/list.json?id=" + parentId,function(json){
			treeNode0=json;
			//初始化root节点
			$.getJSON("/server/department/info/list.json?parentId=" + parentId,function(json){
					for(var i=0;i<treeNode0.length;i++){
					json.push(treeNode0[i]);
					}
				treeNodes = $.fn.zTree.init(anchor, orgTreeSetting,json);

				//初始化curNode
				curNode = treeNodes.getNodeByParam("id", parentId, null);

				callbackFunction = callback;
				loadTreeNode(parentId,callback);

			});
	});
}

//初始化树，不包括回调函数
function initSelectTree(parentId,anchor){
	jQuery.ajaxSettings.async = false;
	//初始化root节点
	$.getJSON("/server/department/info/list.json?id=" + parentId,function(json){
		treeNodes = $.fn.zTree.init(anchor, orgSelectTree,json);
		var node = treeNodes.getNodeByParam("id", parentId, null);
		treeNodes.expandNode(node, true, false, true);
	});
			
}

function delTreeNode(id,parentId,callback){
	$.post("/server/department/info/delete.json?",{id:id},function(json){
		removeNode(id);
		loadTreeNode(parentId,callback);		
	});
}
